/**
 * 项目版本删除接口测试用例
 *
 * Created by snowingsea on 2020/02/13.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');

module.exports = describe('/DELETE project-versions/:id.json', function () {
  function url(versionId) {
    return `/project-versions/${versionId}.json`;
  }
  const administrator = new Administrator();
  let project;
  const newUserIds = [];

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'delete');
  common.should.be.tester(uri, url, 'delete');

  // 正常删除
  it('it should DELETE a project version', function (done) {
    let projectVersionId;
    project.createRandomVersion()
      .then(({ versionId }) => {
        projectVersionId = versionId;
        return project.creator.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(projectVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            // 查询
            chai.request(uri)
              .get(url(projectVersionId))
              .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(404);
                common.should.be.errorMessage(res.body);
                done();
              });
          });
      })
      .catch(done);
  });

  // 项目版本被删光后项目不应被删除
  it('it should not delete a project when the last version is deleted', function (done) {
    let projectId;
    let projectVersionId;
    project.creator.createRandomProject()
      .then((project) => {
        projectId = project.projectId;
        projectVersionId = project.lastVersion.versionId;
        return project.creator.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(projectVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            // 查询项目
            chai.request(uri)
              .get(`/projects/${projectId}.json`)
              .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(200);
                common.should.be.projectMessage(res.body);
                res.body.should.not.have.property('lastVersion');
                done();
              });
          });
      })
      .catch(done);
  });

  // lastVersion被删后所在项目的lastVersion应当更新
  it('it should update lastVersion when lastVersion is deleted', function (done) {
    let newProject;
    let preVersionId;
    let lastVersionId;
    project.creator.createRandomProject()
      .then((project) => {
        newProject = project;
        preVersionId = project.lastVersion.versionId;
        return newProject.createRandomVersion();
      })
      .then(({ versionId }) => {
        lastVersionId = versionId;
        return project.creator.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(lastVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            // 查询项目
            chai.request(uri)
              .get(`/projects/${newProject.projectId}.json`)
              .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(200);
                common.should.be.projectMessage(res.body);
                res.body.should.have.property('lastVersion');
                res.body.lastVersion.versionId.should.be.eql(preVersionId);
                done();
              });
          });
      })
      .catch(done);
  });

  // 删除不存在的项目版本
  it('it should fail to delete a project version when the version is not exists', function (done) {
    let projectVersionId;
    project.createRandomVersion()
      .then(({ versionId }) => {
        projectVersionId = versionId;
        return project.removeVersion(versionId);
      })
      .then(() => project.creator.login())
      .then(() => {
        chai.request(uri)
          .delete(url(projectVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可删除他人的项目版本
  it('it should fail to delete a project version when the version is not yours', function (done) {
    let otherVersionId;
    administrator.createRandomTester()
      .then((newTester) => {
        newUserIds.push(newTester.userId);
        return newTester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then(({ versionId }) => {
        otherVersionId = versionId;
        return project.creator.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(otherVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 冻结的项目不可删除版本
  it('it should fail to delete a project version when the project is frozen', function (done) {
    let newProjectId;
    let newVersionId;
    project.creator.createRandomProject()
      .then((project) => {
        newProjectId = project.projectId;
        return project.createRandomVersion();
      })
      .then(({ versionId }) => {
        newVersionId = versionId;
        return project.creator.freezeProject(newProjectId);
      })
      .then(() => project.creator.login())
      .then(() => {
        chai.request(uri)
          .delete(url(newVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(403);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
